//
//  ZYChatFrame.m
//  Tourism
//
//  Created by 张亚 on 16/1/25.
//  Copyright © 2016年 zhangya. All rights reserved.
//

#import "ZYChatFrame.h"
#import "ZYChatMode.h"
#import "NSString+Extension.h"


#define ZYNameFont [UIFont systemFontOfSize:15]

@implementation ZYChatFrame

- (CGSize) sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{
    NSDictionary * attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;

}

- (void)setChatMode:(ZYChatMode *)chatMode{
    _chatMode = chatMode;
    CGFloat padding = 10;
    //时间位置
    if (chatMode.hideTime == NO) {
        CGFloat timeX = 0;
        CGFloat timeY = 0;
        CGFloat timeW = SCREEN_WIDTH;
        CGFloat timeH = 40;
        _timeF = CGRectMake(timeX, timeY, timeW, timeH);
    }
    //头像位置
    CGFloat iconY = CGRectGetMaxY(_timeF);
    CGFloat iconX;
    CGFloat iconW = 60;
    CGFloat iconH = 60;
    if (chatMode.type == ZYChatModeTypeOther) {
        iconX = padding;
    }else{
        iconX = SCREEN_WIDTH - padding - iconW;
    }
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    //正文位置
    CGFloat textX;
    CGFloat textY = iconY;
    //文字计算的最大尺寸
    CGSize textMaxSize = CGSizeMake(200, MAXFLOAT);
    //文字真实的尺寸大小
    CGSize textRealSize = [chatMode.text sizeWithFont:ZYNameFont maxSize:textMaxSize];
    //把文字放入按钮中
    CGSize textBtnSize = CGSizeMake(textRealSize.width + ZYTextPadding * 2,textRealSize.height + ZYTextPadding * 2);
    
    if (chatMode.type == ZYChatModeTypeOther) {
        textX = CGRectGetMaxX(_iconF) + padding;
    }else{
        textX = iconX - padding - textBtnSize.width;
    }
    _textF = CGRectMake(textX, textY, textBtnSize.width, textBtnSize.height);
    
    CGFloat textMaxY = CGRectGetMaxY(_textF);
    CGFloat iconMaxY = CGRectGetMaxY(_iconF);
    _cellHeight = MAX(textMaxY, iconMaxY) + padding;
}



@end
